//
//  ESUNLoadingView.m
//  ESUN
//
//  Created by wang yongfeng on 14-1-10.
//  Copyright (c) 2014年 wang yongfeng. All rights reserved.
//

#import "ESUNLoadingView.h"
#import <QuartzCore/QuartzCore.h>
@implementation ESUNLoadingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (ESUNLoadingView *)initWithTitle:(NSString *)loading superView:(UIView*)delegate
{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if(self != nil)
    {
        [self setUserInteractionEnabled:YES];
        [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
        contentView = [[UIImageView alloc] init];
        [contentView setFrame:CGRectMake(0, 0, 100, 100)];
        [contentView setImage:[UIImage imageNamed:@"progress_bg"]];
        contentView.center = self.center;
        [self addSubview:contentView];
        //logo
        UIImageView *logo = [[UIImageView alloc] init];
        [logo setFrame:CGRectMake(0, 0, 30, 30)];
        [logo setImage:[UIImage imageNamed:@"progress_logo"]];
        logo.center = CGPointMake(50, 35);
        [contentView addSubview:logo];
        //转动
        loadingImage = [[UIImageView alloc] init];
        [loadingImage setFrame:CGRectMake(0, 0, 50, 50)];
        loadingImage.center = CGPointMake(50, 35);
        [loadingImage setImage:[UIImage imageNamed:@"progress"]];
        [contentView addSubview:loadingImage];
        
        timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(showLoading) userInfo:nil repeats:YES];
        [timer setFireDate:[NSDate distantFuture]];
        
        _delegate = delegate;
        self.loadingLabel = [[UILabel alloc] init];
        [self.loadingLabel setTextAlignment:NSTextAlignmentCenter];
        [self.loadingLabel setFont:[UIFont systemFontOfSize:15]];
        [self.loadingLabel setFrame:CGRectMake(0, 60, 100, 30)];
        [self.loadingLabel setTextColor:[UIColor whiteColor]];
        [self.loadingLabel setBackgroundColor:[UIColor clearColor]];
        [self.loadingLabel setText:loading];
        [contentView addSubview:self.loadingLabel];
        
    }
    return  self;
}

/*
 *显示加载
 */
- (void)show
{
    [self showLoading];
    [timer setFireDate:[NSDate distantPast]];
    contentView.alpha = 1;
    [_delegate addSubview:self];
}

/*
 *加载成功后删除
 */
- (void)hide
{
    [timer setFireDate:[NSDate distantFuture]];
    [UIView animateWithDuration:0.3 animations:^{
        contentView.alpha = 0;
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];
}

- (void)showLoading
{
    static BOOL flag = YES;
    flag = !flag;
    [UIView animateWithDuration:0.05 animations:^{
        CATransform3D  t = loadingImage.layer.transform;
        loadingImage.layer.transform = CATransform3DRotate(t, M_PI_4/4.0, 0, 0, 1);
    }completion:^(BOOL finished) {
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
